﻿using SbsSW.SwiPlCs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Maszyna
{
    public class SWIEngine : INotifyPropertyChanged
    {
        public  string default_path = "C:\\pl\\";
        public  int version = 32;
        public  string path;
        public bool is_connected;

        public event PropertyChangedEventHandler PropertyChanged;
        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        //{
        //    get { return path; }
        //    set
        //    {
        //        if (Directory.Exists(value))
        //        {
        //            path = value;

        //        }
        //    }
        //}
        public  List<string> Code = new List<string>();


        public void disconnect()
        {
            PlEngine.PlCleanup();
            
        }



        private  bool isProperDirectory()
        {
            if (File.Exists(string.Format("{0}\\{1}",path , "boot"+version.ToString() +".prc")))
                return true;

          
            return false;
        }


        private bool isProperDirectory64()
        {
            if (File.Exists(string.Format("{0}\\{1}", path, "boot64.prc")))
                return true;


            return false;
        }

        public SWIEngine()
        {
            init(default_path);
        }
        public void re_init()
        {
            FolderBrowserDialog f = new FolderBrowserDialog();
            MessageBox.Show("Brak silnika SWI Prolog w lokalizacji.");
            if (DialogResult.OK == f.ShowDialog())
                path = f.SelectedPath;
           
            bool test = isProperDirectory();
            bool test2 = isProperDirectory64();
            Environment.SetEnvironmentVariable("SWI_HOME_DIR", path);
            Environment.SetEnvironmentVariable("PATH", String.Format(@"{0}\bin", path));
            Environment.SetEnvironmentVariable("LIB", String.Format(@"{0}\lib", path));
            string eng_path = Path.Combine(Application.StartupPath, "Engine.pl");
            eng_path = eng_path.Replace("\\", "\\\\");
            Code.Add(eng_path);
            try
            {

                PlEngine.Initialize(new string[] { "-q" });
                foreach (string c in Code)
                    PlQuery.PlCall(c);
                is_connected = true;

            }
            catch (FileNotFoundException e)
            {
                FolderBrowserDialog f2 = new FolderBrowserDialog();
                MessageBox.Show("Brak silnika SWI Prolog w lokalizacji.");
                if (DialogResult.OK == f2.ShowDialog())
                    path = f2.SelectedPath;
                init();
            }
            catch (AccessViolationException e)
            {

            }

            if (PlEngine.IsInitialized)
            {
                foreach (string c in Code)
                    PlQuery.PlCall(c);
            }
        }


        public  void init()
        {

            bool test = isProperDirectory();
            bool test2 = isProperDirectory64();
            Environment.SetEnvironmentVariable("SWI_HOME_DIR", path);
            Environment.SetEnvironmentVariable("PATH", String.Format(@"{0}\bin", path));
            Environment.SetEnvironmentVariable("LIB", String.Format(@"{0}\lib", path));
            string eng_path = Path.Combine(Application.StartupPath, "ram.pl");
            eng_path = eng_path.Replace("\\", "\\\\");
            
            
            try
            {

                PlEngine.Initialize(new string[] { "-q" , "-f" , eng_path});
                is_connected = true;
                 
            }
            catch (FileNotFoundException e)
            {
                FolderBrowserDialog f = new FolderBrowserDialog();
                MessageBox.Show("Brak silnika SWI Prolog w lokalizacji.");
                if (DialogResult.OK == f.ShowDialog())
                    path = f.SelectedPath;
                init();
            }
            catch (AccessViolationException e)
            {
           
            }

       
        }


        public  void init(String newPath)
        {
            path = newPath;

            bool test = isProperDirectory();
            bool test2 = isProperDirectory64();
            Environment.SetEnvironmentVariable("SWI_HOME_DIR", path);
            Environment.SetEnvironmentVariable("PATH", String.Format(@"{0}\\bin", path));
            Environment.SetEnvironmentVariable("LIB", String.Format(@"{0}\\lib", path));
            string eng_path = Path.Combine(Application.StartupPath, "ram.pl");
            eng_path = eng_path.Replace("\\", "\\\\");
            try
            {

                PlEngine.Initialize(new string[] { "-q", "-f", eng_path });
          
                is_connected = true;
            }
            catch (FileNotFoundException e)
            {
                FolderBrowserDialog f = new FolderBrowserDialog();
                MessageBox.Show("Brak silnika SWI Prolog w lokalizacji.");
                if (DialogResult.OK == f.ShowDialog())
                    path = f.SelectedPath;
                init();
            }
            catch (AccessViolationException e)
            {
          
            }
            
        }

    }
}
